package com.gton.person.controller;

import com.gton.person.commons.utils.OssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: PersonalDesign
 * @description: 阿里云OSS控制器
 * @author: GuoTong
 * @create: 2020-11-25 15:23
 **/
@RestController
@RequestMapping("oss")
@Slf4j
@Api(value = "OSS", tags = "OSS上传接口")
public class OssController {
    /**
     * 1、注入OssUtil
     */
    @Autowired
    private OssUtil ossUtil;

    /**
     * 普通的文件上传：阿里云OSS存储用户的头像
     *
     * @param file
     * @return
     */

    @PostMapping("/uploadfile")
    @ApiOperation(value = "上传", notes = "上传")
    public Object fileUpload(@RequestParam MultipartFile file) {
        try {
            //2、调用
            String url = ossUtil.uploadFile(file);
            log.info("注册在阿里云OSS：图片");
            Map<String, Object> returnbody = new HashMap<>();
            Map<String, Object> returnMap = new HashMap<>();
            returnMap.put("url", url);
            returnbody.put("data", returnMap);
            returnbody.put("code", "200");
            returnbody.put("message", "上传成功");
            return returnbody;
        } catch (Exception e) {
            Map<String, Object> returnbody = new HashMap<>();
            returnbody.put("data", null);
            returnbody.put("code", "400");
            returnbody.put("message", "上传失败");
            return returnbody;
        }

    }


}
